"use strict";

var operate = {
	field: 'operate',
	title: '操作',
	events: {
		'click .fn_data': fn_data,
		'click .fn_attachment': fn_attachment,
		'click .fn_meta': fn_meta,
		'click .fn_log': fn_log,
		'click .fn_map': fn_map
	},
	formatter: function(value, row, index) {
		return [
			'<a class="fn_data tr_link" href="#" data-toggle="tooltip" title="数据"><i class="fa fa-table"></i></a>',
			'<a class="fn_attachment tr_link" href="#" data-toggle="tooltip" title="附件"><i class="fa fa-paperclip"></i></a>',
			'<a class="fn_meta tr_link" href="#" data-toggle="tooltip" title="元数据"><i class="fa fa-tags"></i></a>',
			'<a class="fn_log tr_link" href="#" data-toggle="tooltip" title="审核记录"><i class="fa fa-list-alt"></i></a>',
			'<a class="fn_map tr_link" href="#" data-toggle="tooltip" title="日志轨迹"><i class="fa fa-globe"></i></a>'
		].join('   ');
	}
};

function fn_map(e, value, row, index) {
	bDialog.open({
		title: "查看日志轨迹",
		dom: '<div id="log_map"></div>',
		customClass: 'blue',
		width: $(window).width() * 0.8,
		height: $(window).height() * 0.9,
		scroll: false,
		onShow: function(dialog) {
			var $map = $(dialog.dialog[0]).find('#log_map');
			$map.append("<div id='mapMask'><div id='mapMaskText'>正在加载数据，请稍候。。。</div></div>")
			$("#mapMask").css({
				"position": "absolute",
				"top": "0",
				"left": "0",
				"width": "100%",
				"height": "100%",
				"background": "#FFFFFF",
				"opacity": "0.3",
				"z-index": "999999"
			});
			$("#mapMaskText").css({
				"position": "absolute",
				"top": "50%",
				"left": "35%",
				"font-size": "28px"
			});

			$map.vw_map({
				height: $(window).height() * 0.9 - 50,
				defaultMapview: VW_INTERFACE.DEFAULT_MAP_VIEW,
				onInitialized: function() {
					var code = row['对象编码'];
					vw_common.customAjax({
						url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetObject'),
						type: "GET",
						dataType: "json",
						data: {
							"catalogID": code
						}
					}, function(e) {
						$map.vw_map('refresh').vw_map("drawPolygon", e.data, true);

						vw_common.customAjax({
							url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetNatureLand'),
							type: "GET",
							dataType: "json",
							data: {
								"code": code
							}
						}, function(e) {
							$map.vw_map("drawPolygon", e.data, false, {
								color: "#FFFACD",
								weight: 1
							});
						});

						vw_common.customAjax({
							url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetDataLog'),
							type: "GET",
							dataType: "json",
							data: {
								"id": row[_idField]
							}
						}, function(e) {

							vw_common.customAjax({
								url: VW_INTERFACE.WEBSERVICE.TableManage('GetTableData'),
								type: "GET",
								dataType: "json",
								data: {
									table: "TB_DIC_OPERATE_TYPE"
								}
							}, function(ee) {
								var dic = {};
								$.each(ee.data, function(k, val) {
									dic[val[0]] = {
										id: k,
										text: val[2]
									};
								});
								e.data.head.id = 0;
								e.data.head.enums = {
									key: 5,
									dic: dic
								};
								$map.vw_map("drawPoint", e.data, false, true, 8);
								$("#mapMask").remove();
							});

						});
					});
				}
			});
			$map.css('margin-top', "0");
		}
	});
}

function fn_data(e, value, row, index) {
	bDialog.open({
		title: "查看数据",
		dom: '<div id="table_viewData"></div>',
		customClass: 'blue',
		width: $(window).width() * 0.8,
		height: $(window).height() * 0.9,
		scroll: false,
		onShow: function(dialog) {
			$(dialog.dialog[0]).find('#table_viewData').vw_edittable({
				type: "table",
				height: $(window).height() * 0.9 - 70,
				showInsertData: false,
				showErrorlog: false,
				showNewData: false,
				showEdit: false,
				showCheckColumn: false,
				dataURL: VW_INTERFACE.WEBSERVICE.DataVerify("LoadTableData"),
				extrapara: {
					id: row[_idField]
				}
			}).vw_edittable('ajaxLoadTableData', row[_idField], "");
		}
	});
}

function fn_attachment(e, value, row, index) {
	var dom = [
		'<div class="row">',
		'	<div style="float: right;font-size: 16px;margin-right: 40px;">附件数量：<span id="num" style="color: red"></span></div>',
		'</div>',
		'<div class="row">',
		'	<p class="file_type">图片</p>',
		'	<div id="pic_file" class="col-md-12"></div>',
		'</div>',
		'<div class="row">',
		'	<p class="file_type">PDF</p>',
		'	<div id="pdf_file" class="col-md-12"></div>',
		'</div>',
		'<div class="row">',
		'	<p class="file_type">其他</p>',
		'	<div id="other_file" class="col-md-12"></div>',
		'</div>'
	].join('');

	bDialog.open({
		title: "查看附件",
		dom: dom,
		customClass: 'blue',
		width: $(window).width() * 0.8,
		height: $(window).height() * 0.9,
		scroll: true,
		onShow: function(dialog) {
			vw_common.customAjax({
				url: VW_INTERFACE.WEBSERVICE.DataVerify('ExamineAnnex'),
				type: "GET",
				dataType: "json",
				data: {
					id: row[_idField]
				}
			}, function(e) {
				$('.file_type').css({
					"display": "inline-block",
					"background-color": "#E9EFF4",
					"padding": "8px 14px",
					"color": "#4b8df8",
					"border-left": "3px solid #4b8df8",
					"margin-left": "16px",
					"margin-top": "3px"
				});

				if(e.picture.length > 0) {
					GenrateAttachment(e.picture, "pic_file");
				}
				if(e.pdf.length > 0) {
					GenrateAttachment(e.pdf, "pdf_file");
				}
				if(e.otherFile.length > 0) {
					GenrateAttachment(e.otherFile, "other_file");
				}

				$(dialog.dialog[0]).find("#num").html(e.picture.length + e.pdf.length + e.otherFile.length);
			});
		}
	});
}

function GenrateAttachment(files, type) {
	var html = "";
	$.each(files, function(k, file) {
		if(k % 4 == 0) {
			html += '<div class="row">';
		}

		var url = file.url;
		if(type == "pdf_file") {
			url = "image/pdf.jpg";
		} else if(type == "other_file") {
			url = "image/otherFile.jpg";
		}

		var name = file.name;
		if(name.length > 6) {
			name = file.name.substr(0, 4) + "...";
		}

		html += [
			'<div class="col-sm-3 col-md-3">',
			'	<div class="thumbnail">' +
			'		<img src=' + url + ' title=' + file.name + ' value=' + file.url + ' onclick="show(this);" style="cursor: pointer;">',
			'		<div class="caption">',
			'			<h4 style="text-align: center">' + name + '</h4>',
			'		</div>',
			'	</div>',
			'</div>'
		].join('');

		if(k % 4 == 3) {
			html += '</div>';
		}
	});

	$("#" + type).append(html);
}

function show(dom) {
	window.open($(dom).attr("value"));
}

function fn_meta(e, value, row, index) {
	bDialog.open({
		title: "查看元数据",
		dom: '<form id="form_meta" class="form-horizontal"></form>',
		customClass: 'blue',
		width: 500,
		height: 400,
		dialogMaxButton: false,
		scroll: true,
		onShow: function(dialog) {
			vw_common.customAjax({
				url: VW_INTERFACE.WEBSERVICE.DataVerify('ExamineMeta'),
				type: "GET",
				dataType: "json",
				data: {
					userID: _userID,
					id: row[_idField]
				}
			}, function(e) {
				var $formMeta = $(dialog.dialog[0]).find('#form_meta');
				$.each(e.reportMeta, function(k, section) {
					var html = '<p class="form-section" ><span class="form-span meta-section" style="font-weight:bold">' + section.type + '</span></p>';
					var data = section.data;
					for(var i = 0; i < data.length; i++) {
						html += '<div class="form-group">';
						html += '<label class="col-xs-3 control-label">' + data[i].chField + ': </label>';
						html += '<div class="col-xs-8">';
						var disabled = "disabled";
						switch(data[i].type) {
							case "number":
								html += '<input type="number" class="form-control" name="' + data[i].egField + '" id="' + data[i].chField + '" value="' + data[i].value + '"' + disabled + '/>';
								break;
							case "string":
								html += '<input type="text" class="form-control" name="' + data[i].egField + '" id="' + data[i].chField + '" value="' + data[i].value + '"' + disabled + '/>';
								break;
							case "textarea":
								html += '<textarea class="form-control" name="' + data[i].egField + '" id="' + data[i].chField + '" ' + disabled + '>' + data[i].value + '</textarea>';
								break;
							case "":
								html += '<input type="text" class="form-control" name="' + data[i].egField + '" id="' + data[i].chField + '" value="' + data[i].value + '"' + disabled + '/>';
								break;
							case "number_dic":
								html += '<select class="form-control" name="' + data[i].egField + '" style="width:100%;"' + disabled + '></select>';
								break;
							case "date":
								var dateValue = data[i].value ? data[i].value : vw_static.getNowDate();
								html += '<input type="date" class="form-control" name="' + data[i].egField + '" id="' + data[i].chField + '" value="' + dateValue + '"' + disabled + '/>';
								break;
							default:
						}
						html += '</div></div>';
					}
					$formMeta.append(html);

					$.each(data, function(k, val) {
						if(val.type == "number_dic") {
							$("select[name = '" + val.egField + "']").select2({
								data: val.values,
							});
							$("select[name = '" + val.egField + "']").val(val.value).trigger('change');
						}
					});
				});
			});
		}
	})
}

function fn_log(e, value, row, index) {
	bDialog.open({
		title: "查看审核记录",
		dom: '<div id="table_stat"></div>',
		customClass: 'blue',
		width: $(window).width() * 0.8,
		height: $(window).height() * 0.9,
		scroll: false,
		onShow: function(dialog) {
			$(dialog.dialog[0]).find('#table_stat').vw_edittable({
				type: "table",
				height: $(window).height() * 0.9 - 70,
				showInsertData: false,
				showErrorlog: false,
				showNewData: false,
				showEdit: false,
				showCheckColumn: false,
				columnFormatter: _columnFormatter,
				dataURL: VW_INTERFACE.WEBSERVICE.DataVerify("ExamineLog"),
				extrapara: {
					id: row[_idField]
				}
			}).vw_edittable('ajaxLoadTableData', row[_idField], "");
		}
	});
}